<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class indexModule extends StoreBaseModule
{
	public function index()
	{
		convert_req($_REQUEST);	
		$keyword = addslashes(htmlspecialchars(trim($_REQUEST['keyword'])));
		//获取当前页的团购商品列表
		require_once APP_ROOT_PATH.'app/Lib/page.php';
		$city_id = intval($GLOBALS['deal_city']['id']);
		//分页
		$page = intval($_REQUEST['p']);
		if($page==0)
		$page = 1;
		$limit = (($page-1)*app_conf("DEAL_PAGE_SIZE")).",".app_conf("DEAL_PAGE_SIZE");	
				
			
		$cid = intval($_REQUEST['cid']);
		$uname = addslashes(trim($_REQUEST['cid']));
		$cate_item = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."deal_cate where id = ".$cid." or (uname = '".$uname."' and uname <> '')");
					
		if($keyword !=''){
			$condition=" deal_type in(0,1)";//这样可以搜索到KTV商家(13/4/2 chh)
		}else
		{
			$condition = " deal_type=0 ";  //条件
		}
		
		$base_param = $url_param = array(
				"cid"	=> addslashes(trim($_REQUEST['cid'])),
				"aid"	=>	intval($_REQUEST['aid']),
				"tid"	=>	intval($_REQUEST['tid']),
				"qid"	=>	intval($_REQUEST['qid']),
				"pid"	=>	intval($_REQUEST['pid']),
				"sid"	=>	intval($_REQUEST['sid']),
				"a"	=>	intval($_REQUEST['a']),
				"keyword"	=>	$keyword,
		);			
		//组装分组筛选
		if($_REQUEST['g']&&is_array($_REQUEST['g']))
		foreach ($_REQUEST['g'] as $k=>$v)
		{
			$url_param["g[".$k."]"] = addslashes(trim($v));
		}

		
		if(intval($_REQUEST['is_redirect'])==1)
		{
			app_redirect(url("store","index",$url_param));
		}
		$GLOBALS['tmpl']->assign("url_param",$url_param); //将变量输出到模板
			
		unset($url_param['keyword']);
		unset($url_param['minprice']);
		unset($url_param['maxprice']);
		$cache_param = $url_param;
		$cache_param['cid'] = $cate_item['id'];
		$cache_param['city_id'] = $city_id;
		$cache_param['deal_type'] = 0;
				
		$result = load_auto_cache("store_filter_nav_cache",$cache_param);
		
		$GLOBALS['tmpl']->assign("cate_all",$result['cate_all']);
		$GLOBALS['tmpl']->assign("area_all",$result['area_all']);
		
		$filter_url_param = $base_param;
		$filter_url_param['has_tuan'] = intval($_REQUEST['has_tuan']);
		$filter_url_param['has_daijin'] = intval($_REQUEST['has_daijin']);
		$filter_url_param['has_youhui'] = intval($_REQUEST['has_youhui']);
		$filter_url_param['has_event'] = intval($_REQUEST['has_event']);
		$filter_url_param['has_goods'] = intval($_REQUEST['has_goods']);
		$filter_url_param['is_verify'] = intval($_REQUEST['is_verify']);
		
		$GLOBALS['tmpl']->assign("filter_url_param",$filter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_tuan']==1)
		$tmpfilter_url_param['has_tuan'] = 0;
		else
		$tmpfilter_url_param['has_tuan'] = 1;
		$filter_url['has_tuan'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_daijin']==1)
		$tmpfilter_url_param['has_daijin'] = 0;
		else
		$tmpfilter_url_param['has_daijin'] = 1;
		$filter_url['has_daijin'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_youhui']==1)
		$tmpfilter_url_param['has_youhui'] = 0;
		else
		$tmpfilter_url_param['has_youhui'] = 1;
		$filter_url['has_youhui'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_event']==1)
		$tmpfilter_url_param['has_event'] = 0;
		else
		$tmpfilter_url_param['has_event'] = 1;
		$filter_url['has_event'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_goods']==1)
		$tmpfilter_url_param['has_goods'] = 0;
		else
		$tmpfilter_url_param['has_goods'] = 1;
		$filter_url['has_goods'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['is_verify']==1)
		$tmpfilter_url_param['is_verify'] = 0;
		else
		$tmpfilter_url_param['is_verify'] = 1;
		$filter_url['is_verify'] = url("store","index",$tmpfilter_url_param);
		
		
		$GLOBALS['tmpl']->assign("filter_url",$filter_url);
		//输出大区	
		$seo_title = $GLOBALS['lang']['STORE_LIST'];
		$seo_keyword = $GLOBALS['lang']['STORE_LIST'];
		$seo_description = $GLOBALS['lang']['STORE_LIST'];
		$append_seo = "";
		$area_id = intval($_REQUEST['aid']);	
		$quan_id = intval($_REQUEST['qid']);
		if($area_id>0)
		{
			$area_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."area where id = ".$area_id);	
				$append_seo = $area_name;
			if($quan_id>0)
			{
							
				$kw_unicode = str_to_unicode_string($area_name);
				//有筛选
				$condition .=" and (match(locate_match) against('".$kw_unicode."' IN BOOLEAN MODE))";	
				
			}
			else
			{
				$ids = load_auto_cache("deal_quan_ids",array("quan_id"=>$area_id));
				$quan_list = $GLOBALS['db']->getAll("select `name` from ".DB_PREFIX."area where id in (".implode(",",$ids).")");
				$unicode_quans = array();
				foreach($quan_list as $k=>$v){
					$unicode_quans[] = str_to_unicode_string($v['name']);
				}
				$kw_unicode = implode(" ", $unicode_quans);
				$condition .= " and (match(locate_match) against('".$kw_unicode."' IN BOOLEAN MODE))";
			}	
		}
		
		$GLOBALS['tmpl']->assign("area_list",$result['area_list']);
		
		if($area_id>0)
		{
			//输出商圈
	
			if($quan_id>0)
			{
					$area_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."area where id = ".$quan_id);				
					$kw_unicode = str_to_unicode_string($area_name);
					//有筛选
					$condition .=" and (match(locate_match) against('".$kw_unicode."' IN BOOLEAN MODE)) ";	
					$append_seo = $append_seo.$area_name;			
			}
			
			$GLOBALS['tmpl']->assign("quan_list",$result['quan_list']);
		}
		
		
		 $avg_price = intval($_REQUEST['a']);
		 if($avg_price>0)
		 {
		   		$condition .=" and avg_price = ".$avg_price;
				$append_seo .= "人均".$GLOBALS['lang']['AVG_PRICE_'.$avg_price]."的";
		 }
		 $GLOBALS['tmpl']->assign("avg_price_all",$result['avg_price_all']);
		 $GLOBALS['tmpl']->assign("avg_price_list",$result['avg_price_list']);
		
		//输出分类
		$cate_id = $cate_item['id'];
		
		$GLOBALS['tmpl']->assign("cate_list",$result['cate_list']);	
		
		//输出小分类
		$deal_type_id = intval($_REQUEST['tid']);	
		$deal_cate_id = $cate_id;
		if($deal_cate_id>0)
		{			
			$GLOBALS['tmpl']->assign("scate_list",$result['scate_list']);			
			$GLOBALS['tmpl']->assign("tag_group",$result['tag_group']);
			
			$deal_cate_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."deal_cate where id = ".$deal_cate_id);			
			$deal_cate_name_unicode = str_to_unicode_string($deal_cate_name);
			$condition .= " and (match(deal_cate_match) against('".$deal_cate_name_unicode."' IN BOOLEAN MODE)) ";
			
			$cate_condition = " (match(deal_cate_match) against('".$deal_cate_name_unicode."' IN BOOLEAN MODE)) ";
			
			$append_seo.=$cate_item['name'];		
		}
			
		if($deal_type_id>0)
		{
			$deal_type_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."deal_cate_type where id = ".$deal_type_id);
			$deal_type_name_unicode = str_to_unicode_string($deal_type_name);
			$condition .= " and (match(deal_cate_match) against('".$deal_type_name_unicode."' IN BOOLEAN MODE)) ";
			$append_seo.=$deal_type_name;
		}
		
		//输出目的
		$purpose_id =  intval($_REQUEST['pid']);
		$purpose_type_id =  intval($_REQUEST['sid']);
		if($purpose_id > 0){
			$unicode_purpose = array();
			$purpose_name= $GLOBALS['db']->getOne("select name from ".DB_PREFIX."purpose_cate where id = ".$purpose_id);
			$append_seo = $append_seo.$purpose_name;
			$unicode_purpose[] = str_to_unicode_string(str_replace("，","",$purpose_name));
			$kw_unicode = implode(" ", $unicode_purpose);
			if($purpose_type_id > 0){
				$unicode_purpose = array();
				$purpose_type_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."purpose_cate_type where id = ".$purpose_type_id);
				$unicode_purpose[] = str_to_unicode_string(str_replace("，","",$purpose_type_name));
				$kw_unicode = implode(" ", $unicode_purpose);
				$append_seo = $append_seo.$purpose_type_name;
			}
			$condition .= " and (match(purpose_match) against('".$kw_unicode."' IN BOOLEAN MODE))";
			$GLOBALS['tmpl']->assign("spurpose_list",$result['spurpose_list']);
		}
		
		$GLOBALS['tmpl']->assign("purpose_all",$result['purpose_all']);
		$GLOBALS['tmpl']->assign("purpose_list",$result['purpose_list']);
		
			
		$seo_title = $append_seo.$seo_title;
	    $seo_keyword = $append_seo.$seo_keyword;
	    $seo_description = $append_seo.$seo_keyword;	
		
	   $supplier_id = intval($_REQUEST['id']);
	  
	   if($supplier_id>0)
	   {
	   	   $supplier_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier where id = ".$supplier_id);
	   	   if($supplier_info)
	   	   {
	   	   	  $GLOBALS['tmpl']->assign("supplier_info",$supplier_info);
	   	   	  $condition.=" and supplier_id = ".$supplier_id;
	   	   	  
	   	   }
	   }
	   
	   $url_param['tag'] = addslashes($_REQUEST['tag']);
	   if($url_param['tag']){
	   		$kw_unicode = str_to_unicode_string($url_param['tag']);
			//有筛选
			$condition .=" and (match(tags_match) against('".$kw_unicode."' IN BOOLEAN MODE))";	
	   }
	   
	   if($_REQUEST['g']&&is_array($_REQUEST['g']))
	   {
	   		foreach($_REQUEST['g'] as $k=>$v)
	   		{
	   			if(trim($v)!="")
	   			{
		   			$kw_unicode = str_to_unicode_string($v);
		   			$condition .=" and (match(tags_match) against('".$kw_unicode."' IN BOOLEAN MODE))";	
	   			}
	   		}
	   }
	   
	   if($keyword){
	   		$GLOBALS['tmpl']->assign("keyword",$keyword);
	   		$kws_div = div_str($keyword);
			foreach($kws_div as $k=>$item)
			{
				$kw[$k] = str_to_unicode_string($item);
			}
			$kw_unicode = implode(" ",$kw);
			//有筛选
			$condition .=" and ((match(name_match,locate_match,deal_cate_match,tags_match) against('".$kw_unicode."' IN BOOLEAN MODE)) or name like '%".$keyword."%') ";	
	   }
	   
	
		if($filter_url_param['has_tuan']>0)
		$condition.=" and tuan_count > 0 ";
		
		if($filter_url_param['has_daijin']>0)
		$condition.=" and daijin_count > 0 ";
		
		if($filter_url_param['has_youhui']>0)
		$condition.=" and youhui_count > 0 ";
		
		if($filter_url_param['has_event']>0)
		$condition.=" and event_count > 0 ";
		
		if($filter_url_param['has_goods']>0)
		$condition.=" and shop_count > 0 ";
		
		if($filter_url_param['is_verify']>0)
		$condition.=" and is_verify = 1 ";
		
		
	   	$sort_field = es_cookie::get("store_sort_field");
		$sort_type = es_cookie::get("store_sort_type");
		
		if($sort_field!="default"&&$sort_field!="dp_count"&&$sort_field!="avg_point"&&$sort_field!="ref_avg_price")
		{
			$sort_field = "default";
		}
		if($sort_field=='default')
		{
			$sortby = " is_recommend desc,sort desc,is_verify desc,dp_count desc ";
		}
		else
		{
			$sortby = $sort_field." ".$sort_type;
		}
		
		$GLOBALS['tmpl']->assign('sort_field',$sort_field);
		$GLOBALS['tmpl']->assign('sort_type',$sort_type);
			
	   $result = get_store_list($limit,0,$condition,$sortby,false);
	  
	   foreach($result['list'] as $k=>$v)
	   {
	   	if($v['tuan_count']>0)
	   	$result['list'][$k]['tuan'] = $GLOBALS['db']->getRow("select d.name,d.id from ".DB_PREFIX."deal as d left join ".DB_PREFIX."deal_location_link as l on l.deal_id = d.id where d.is_effect = 1 and d.is_delete = 0 and d.time_status <> 2 and l.location_id = ".$v['id']." order by sort desc limit 1");

	   	if($v['youhui_count']>0)
		$result['list'][$k]['youhui'] = $GLOBALS['db']->getRow("select y.name,y.id from ".DB_PREFIX."youhui as y left join ".DB_PREFIX."youhui_location_link as l on l.youhui_id = y.id where y.is_effect = 1 and (y.end_time = 0 or y.end_time > ".get_gmtime().") and l.location_id = ".$v['id']." order by sort desc limit 1");
	   	
	   }
	   
	   $GLOBALS['tmpl']->assign("list",$result['list']);
	   $page = new Page($result['count'],app_conf("DEAL_PAGE_SIZE"));   //初始化分页对象 		
	   $p  =  $page->show();
	   $GLOBALS['tmpl']->assign('pages',$p);
	   
		
		$site_nav[] = array('name'=>$GLOBALS['lang']['HOME_PAGE'],'url'=>url("index","index"));
		$site_nav[] = array('name'=>$GLOBALS['lang']['STORE_LIST'],'url'=>url("store","index"));
		$GLOBALS['tmpl']->assign("site_nav",$site_nav);
			
		
		//输出最新加入的商家
		$new_stores = get_store_list(5,0,$cate_condition," id desc ",false);
		$GLOBALS['tmpl']->assign("new_stores",$new_stores['list']);
		if(trim($cate_condition)!='')
		$rec_stores = get_store_list(5,0, $cate_condition." and is_recommend = 1 "," id desc ",false);
		else
		$rec_stores = get_store_list(5,0, " is_recommend = 1 "," id desc ",false);
		$GLOBALS['tmpl']->assign("rec_stores",$rec_stores['list']);
					
		$GLOBALS['tmpl']->assign("page_title",$seo_title);

		$GLOBALS['tmpl']->assign("page_keyword",$seo_keyword.",");
				
		$GLOBALS['tmpl']->assign("show_share",1);
		
		$GLOBALS['tmpl']->assign("page_description",$seo_description.",");
		$GLOBALS['tmpl']->display("store_index.html");		
	}
	
	
	
	
	function send_sms_view(){
		$return["status"]=0;
		if(!$GLOBALS['user_info']){
			$return["status"]=2;
			$return["message"]=$GLOBALS['LANG']["PLEASE_LOGIN_FIRST"];
			ajax_return($return);
			exit();
		}
		
		$id = intval($_REQUEST['id']);
		if($id==0){
			$return["message"]=$GLOBALS['LANG']["NO_SUPPLIER"];
			ajax_return($return);
			exit();
		}
		
		$store_info = $GLOBALS['db']->getRow("select id,`name`,`address`,`tel`,`sms_content` from ".DB_PREFIX."supplier_location where id = ".$id);
		if(!$store_info)
		{
			$return["message"]=$GLOBALS['LANG']["NO_SUPPLIER"];
			ajax_return($return);
			exit();
		}		
		
		$return["status"]=1;
		$GLOBALS['tmpl']->assign("store_info",$store_info);
		$return['html'] = $GLOBALS['tmpl']->fetch("inc/store/sms_view_form.html");
		ajax_return($return);
		exit();
	}
	function send_store_sms(){
		$return["status"]=0;
		if(!$GLOBALS['user_info']){
			$return["status"]=2;
			$return["message"]=$GLOBALS['LANG']["PLEASE_LOGIN_FIRST"];
			ajax_return($return);
			exit();
		}
		$id = intval($_REQUEST['store_id']);
		if($id==0){
			$return["status"]=0;
			$return["message"]=$GLOBALS['LANG']["NO_SUPPLIER"];
			ajax_return($return);
			exit();
		}
		$store_info = $GLOBALS['db']->getRow("select `name`,`address`,`tel`,`sms_content` from ".DB_PREFIX."supplier_location where id = ".$id);
		if(!$store_info)
		{
			$return["status"]=0;
			$return["message"]=$GLOBALS['LANG']["NO_SUPPLIER"];
			ajax_return($return);
			exit();
		}	
		
		//手机
		if(!check_mobile($_REQUEST['mobile']))
		{
			$return["status"]=0;
			$return["message"]=$GLOBALS['LANG']["FILL_CORRECT_MOBILE_PHONE"];
			ajax_return($return);
			exit();
		}
		
		//验证码
		if(app_conf("VERIFY_IMAGE")==1)
		{
			$verify = md5(trim($_REQUEST['verify']));
			$session_verify = es_session::get('verify');
			if($verify!=$session_verify)
			{				
				$return["message"]=$GLOBALS['lang']['VERIFY_CODE_ERROR'];
				ajax_return($return);
				exit();
			}
		}
		
		es_session::delete("verify");
		
		$now = get_gmtime();
		$today_begin = to_timespan(to_date($now,"Y-m-d"));
		$today_end = $today_begin + 24*3600;
		$count = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."deal_msg_list where user_id = ".$GLOBALS['user_info']['id']." and is_youhui = 2 and create_time between ".$today_begin." and ".$today_end);

		if($count>=intval(app_conf("STORE_SEND_LIMIT")))
		{
			 $return['message'] = $GLOBALS['lang']['SMS_LIMIT_OVER'];
			 ajax_return($return);
			 exit();
		}
		
		$sms_content = "";
		
		if ($store_info['sms_content']){
			$sms_content = $store_info['sms_content'];
		}
		else{
			$sms_content = $store_info['name'].$store_info['tel'].' '.$store_info['address'];
		}
		
		$msg_data['send_type'] = 0;
		$msg_data['content'] = $sms_content;
		$msg_data['send_time'] = 0;
		$msg_data['is_send'] = 0;
		$msg_data['create_time'] = get_gmtime();
		$msg_data['user_id'] = $GLOBALS['user_info']['id'];
		$msg_data['is_html'] = 0;
		$msg_data['is_youhui'] = 2;
		$msg_data['dest'] = $_REQUEST['mobile'];
		
		$GLOBALS['db']->autoExecute(DB_PREFIX."deal_msg_list",$msg_data); //插入 
		
		$return["status"]=1;
		ajax_return($return);
		exit();
	}
	
	public function vote_tag()
	{
		if($GLOBALS['user_info'])
		{
			$tag_name = addslashes(htmlspecialchars(trim($_REQUEST['tag_name'])));
			$group_id = intval($_REQUEST['group_id']);
			$location_id = intval($_REQUEST['location_id']);
			$user_id = intval($GLOBALS['user_info']['id']);
			$rs = intval($GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."tag_user_vote where user_id = ".$user_id." and group_id = ".$group_id." and location_id = ".$location_id." and tag_name = '".$tag_name."'"));
			if($rs)
			{
				$result['status'] = 0;
				$result['info'] = "您已经对该项投过票了";
				ajax_return($result);
			}
			else
			{
				$data['tag_name'] = $tag_name;
				$data['user_id'] = $user_id;
				$data['group_id'] = $group_id;
				$data['location_id'] = $location_id;
				$GLOBALS['db']->autoExecute(DB_PREFIX."tag_user_vote",$data);
				$GLOBALS['db']->query("update ".DB_PREFIX."supplier_tag set total_count = total_count +1 where tag_name = '".$tag_name."' and supplier_location_id = ".$location_id." and group_id = ".$group_id);
				$count = intval($GLOBALS['db']->getOne("select total_count from ".DB_PREFIX."supplier_tag where tag_name = '".$tag_name."' and supplier_location_id = ".$location_id." and group_id = ".$group_id));
				$result['status'] = 1;
				$result['info'] = $tag_name."(".$count.")";
				$cache_id  = md5("store"."view".$location_id);		
				$GLOBALS['tmpl']->clear_cache('store_view.html', $cache_id);
				ajax_return($result);
			}
		}
		else
		{
			$result['status'] = 2;
			ajax_return($result);
		}
	}
		
	
	public function sign_page()
	{
		$location_id = intval($_REQUEST['id']);
		$store_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location where id = ".$location_id." and is_effect = 1");
		$GLOBALS['tmpl']->assign("store_info",$store_info);	
		$GLOBALS['tmpl']->display("store_sign_page.html");	
	}
	
	public function do_sign(){
		if($GLOBALS['user_info'])
		{
			$point = intval($_REQUEST['point']);
			$id = intval($_REQUEST['id']);
			$user_id = intval($GLOBALS['user_info']['id']);
			if($point<=0||$point>5)
			{
				ajax_return(array("status"=>false,"message"=>"请点击星星为商家打分，最高5颗星"));
			}
			$store_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location where id = ".$id." and is_effect = 1");
			if($store_info)
			{
				$begin_time = get_gmtime() - 3 * 24 * 3600;
				$end_time = get_gmtime() + 3 * 24 * 3600;
				$sign_data_count = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location_sign_log where user_id = ".$user_id." and location_id  = ".$id." and (sign_time between $begin_time and $end_time)");
				if($sign_data_count)
				{
					ajax_return(array("status"=>false,"message"=>"最近一周您已经签到过了"));
				}
				else
				{
					$sign_data['user_id'] = $user_id;
					$sign_data['location_id'] = $id;
					$sign_data['point'] = $point;
					$sign_data['sign_time'] = get_gmtime();
					$GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_sign_log",$sign_data);
					
					
					syn_supplier_locationcount($store_info);
					$cache_id  = md5("store"."view".$store_info['id']);		
					$GLOBALS['tmpl']->clear_cache('store_view.html', $cache_id);
					ajax_return(array("status"=>true,"message"=>"签到成功"));
				}
			}
			else
			{
				ajax_return(array("status"=>false,"message"=>"商户不存在"));
			}
		}
		else
		{
			ajax_return(array("status"=>false,"message"=>"请先登录"));
		}
	}
	
	public function checkfav(){
		if($GLOBALS['user_info'])
		{
			convert_req($_REQUEST);
			$ids = trim($_REQUEST['ids']);
			if($ids==""){
				ajax_return(array("status"=>false,"message"=>""));
			}
			$uid =  $GLOBALS['user_info']['id'];
			$list = $GLOBALS['db']->getAll("SELECT sid FROM ".DB_PREFIX."user_fav_location WHERE sid in(".$ids.") AND uid = ".$uid);
			
			ajax_return(array("status"=>true,"data"=>$list));
		}
		else{
			ajax_return(array("status"=>false,"message"=>"请先登录"));
		}
	}
	
	public function addfav(){
		if($GLOBALS['user_info'])
		{
			convert_req($_REQUEST);
			$id = intval($_REQUEST['id']);
			if($id==0){
				ajax_return(array("status"=>false,"message"=>"请选择餐厅"));
				exit();
			}
			
			$uid =  $GLOBALS['user_info']['id'];
			if($GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."user_fav_location WHERE sid = $id  AND uid = ".$uid) > 0){
				$GLOBALS['db']->query("delete from ".DB_PREFIX."user_fav_location where sid = $id  AND uid = ".$uid);
				ajax_return(array("status"=>2,"message"=>"取消关注成功"));
			}
			else{
				$GLOBALS['db']->autoExecute(DB_PREFIX."user_fav_location",array("sid"=>$id,"uid"=>$uid,"create_time"=>get_gmtime()),"INSERT");
				ajax_return(array("status"=>1,"message"=>"关注成功"));
			}
			
		}
		else{
			ajax_return(array("status"=>false,"message"=>"请先登录"));
		}
	}
	
	public function loadimpressioncount(){
		convert_req($_REQUEST);
		$ids = trim($_REQUEST['ids']);
		if($ids==""){
			ajax_return(array("status"=>false,"message"=>""));
		}
		
		$tlist = $GLOBALS['db']->getAll("SELECT sid,number_count FROM ".DB_PREFIX."location_impression WHERE sid in(".$ids.")");
		$list = array();
		foreach($tlist as $k=>$v){
			$list[$v['sid']]['sid'] = $v['sid'];
			$list[$v['sid']]['number_count'] += (int)$v['number_count'];
		}
		$newlist = array();
		foreach($list as $k=>$v){
			$newlist[] = $v;
		}
		ajax_return(array("status"=>true,"data"=>$newlist));
	}
	
	public function loadimpressionall(){
		convert_req($_REQUEST);
		$id = intval($_REQUEST['id']);
		
		if($id==0){
			ajax_return(array("status"=>false,"message"=>"数据库异常，获取失败"));
		}
		
		$list = $GLOBALS['db']->getAll("SELECT sid,`name`,number_count FROM ".DB_PREFIX."location_impression WHERE sid = ".$id." order by number_count DESC ");
		
		$all_number = 0;
		
		foreach($list as $k=>$v){
			$all_number +=$v['number_count'];
		}
		
		foreach($list as $k=>$v){
			$list[$k]["point"] = sprintf("%01.2f",$v['number_count']/$all_number*100);
		}
		
				
		$GLOBALS['tmpl']->assign("id",$id);
		$GLOBALS['tmpl']->assign("implist",$list);
		
		$html = $GLOBALS['tmpl']->fetch("inc/store/impressionall.html");
		
		ajax_return(array("status"=>true,"html"=>$html));
	}
	public function impression(){
		if(!check_ipop_limit(get_client_ip(),"user_impression",intval(app_conf("SUBMIT_DELAY"))))
			ajax_return(array("status"=>false,"message"=>$GLOBALS['lang']['SUBMIT_TOO_FAST']));
			
		if($GLOBALS['user_info'])
		{
			convert_req($_REQUEST);
			$sid = intval($_REQUEST['sid']);
			$name = trim($_REQUEST['val']);
			if($sid==0){
				ajax_return(array("status"=>false,"message"=>"数据库异常，提交失败"));
			}
			if($name==""){
				ajax_return(array("status"=>false,"message"=>"请输入印象!"));
			}
			
			if($GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."location_impression WHERE sid = ".$sid." and name='".$name."'") > 0){
				$GLOBALS['db']->query("update ".DB_PREFIX."location_impression SET number_count =  number_count+1  WHERE sid = ".$sid." and name='".$name."' " );
				
			}
			else{
				$data['sid'] = $sid;
				$data['name'] = $name;
				$data['number_count'] =  1;
				$data['uid'] =  $GLOBALS['user_info']['id'];
				$GLOBALS['db']->autoExecute(DB_PREFIX."location_impression",$data,"INSERT");
			}
			
			$imp_count = $GLOBALS['db']->getOne("SELECT sum(number_count) FROM ".DB_PREFIX."location_impression WHERE sid = ".$sid);
			$GLOBALS['db']->query("update ".DB_PREFIX."supplier_location SET imp_count =  ".$imp_count."  WHERE id = ".$sid);			
		
			$supplier_info = $GLOBALS['db']->getRow("select name from ".DB_PREFIX."supplier_location where id = ".$sid);
			$title = "对".$supplier_info['name']."发表了印象";
			$url_route = array(
					'rel_app_index'	=>	'store',
					'rel_route'	=>	'view',
					'rel_param' => 'id='.$sid
			);
			$tid = insert_topic($name,$title,"impression",$group="", $relay_id = 0, $fav_id = 0,$group_data = "",$attach_list=array(),$url_route);
			if($tid)
			{
				$GLOBALS['db']->query("update ".DB_PREFIX."topic set source_name = '网站' where id = ".intval($tid));
			}
			
			
			$point = intval(app_conf("USER_IMPRESSION_POINT"));
			$score = intval(app_conf("USER_IMPRESSION_SCORE"));
			$money = doubleval(app_conf("USER_IMPRESSION_MONEY"));
			if($point>0||$score>0||$money>0)
			{
				require_once APP_ROOT_PATH."system/libs/user.php";
				$data = array("money"=>$money,"score"=>$score,"point"=>$point);
				modify_account($data,$GLOBALS['user_info']['id'],"您在".to_date(get_gmtime())."发表了一个印象");
									
			}
			
			ajax_return(array("status"=>true,"message"=>"发表成功，审核后你将会得到一定的奖励!"));
		}
		else{
			ajax_return(array("status"=>2,"message"=>"请先登录"));
		}
	}
}
?>